
package com.android.fitnesscenter.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.TextView;

import com.android.fitnesscenter.R;

public class ViewHelper {
    private Context context;
    public TextView textView;

    public ViewHelper(Context context) {
        this.context = context;
    }

    /**
     * Save the file name of SharedPreference session
     * 
     * @Title: showBTNDialog
     * @param icon
     * @param title
     * @param msg
     * @param btn1_content
     * @param positiveListener
     * @param onCancelListener
     */
    public void showBTNDialog(String msg, String btn1_content,
            DialogInterface.OnClickListener positiveListener,
            DialogInterface.OnCancelListener onCancelListener) {
        if (context == null) {
            return;
        }
        try {

            new AlertDialog.Builder(context).setMessage(msg)
                    .setPositiveButton(btn1_content, positiveListener)
                    .setOnCancelListener(onCancelListener).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Dialog box pop-up message (2 button)
     * 
     * @Title: showBTN2Dialog
     * @param icon
     * @param title
     * @param msg
     * @param btn1_content
     * @param btn2_content
     * @param positiveListener
     * @param negativeListener
     * @param onCancelListener
     */
    public void showBTN2Dialog(String title, String msg, String btn1_content,
            String btn2_content,
            DialogInterface.OnClickListener positiveListener,
            DialogInterface.OnClickListener negativeListener,
            DialogInterface.OnCancelListener onCancelListener) {
        // setCanceledOnTouchOutside
        if (context == null) {
            return;
        }
        AlertDialog alertDialog = new AlertDialog.Builder(context)
                .setTitle(title).setMessage(msg)
                .setPositiveButton(btn1_content, positiveListener)
                .setNegativeButton(btn2_content, negativeListener)
                .setOnCancelListener(onCancelListener).create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }

    public void showBTN1Dialog(String msg, String btn1_content,
            DialogInterface.OnClickListener neutralListener) {
        if (context == null) {
            return;
        }
        try {
            new AlertDialog.Builder(context).setMessage(msg)
                    .setNeutralButton(btn1_content, neutralListener).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showBTNDialog(String msg) {
        if (context == null) {
            return;
        }
        try {
            new AlertDialog.Builder(context)
                    .setMessage(msg)
                    .setPositiveButton(
                            context.getResources().getString(R.string.comm_ok),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    dialog.dismiss();
                                }
                            })
                    .setOnCancelListener(
                            new DialogInterface.OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface dialog) {
                                    dialog.dismiss();
                                }
                            }).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showBTN1Dialog(String msg, final Activity activity) {
        if (context == null) {
            return;
        }
        try {
            new AlertDialog.Builder(context)
                    .setMessage(msg)
                    .setPositiveButton(
                            context.getResources().getString(R.string.comm_ok),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    dialog.dismiss();
                                    activity.finish();
                                }
                            })
                    .setOnCancelListener(
                            new DialogInterface.OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface dialog) {
                                    dialog.dismiss();
                                    activity.finish();
                                }
                            }).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showBTNDialog(String msg, String title) {
        if (context == null) {
            return;
        }
        try {
            new AlertDialog.Builder(context)
                    .setTitle(title)
                    .setMessage(msg)
                    .setPositiveButton(
                            context.getResources().getString(R.string.comm_ok),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    dialog.dismiss();
                                }
                            })
                    .setOnCancelListener(
                            new DialogInterface.OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface dialog) {
                                    dialog.dismiss();
                                }
                            }).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showErrorDialog(Exception ie) {
        if (context == null) {
            return;
        }
        try {
            String msg = context.getString(R.string.comm_no_internet);
            new AlertDialog.Builder(context)
                    .setMessage(msg)
                    .setPositiveButton(
                            context.getResources()
                                    .getString(R.string.comm_back),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    dialog.dismiss();
                                }
                            })
                    .setOnCancelListener(
                            new DialogInterface.OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface dialog) {
                                    dialog.dismiss();
                                }
                            }).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showListDialog(String title, String[] items, OnClickListener clickListener) {
        new AlertDialog.Builder(context)
                .setTitle(title)
                .setItems(items, clickListener)
                .create().show();
    }

    public static Activity getRootContext(Activity activity) {
        while (activity.getParent() != null) {
            activity = activity.getParent();
        }
        return activity;
    }

    public ProgressDialog getProgressDialog(String message) {
        ProgressDialog mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setMessage(message);
        mProgressDialog.setIndeterminate(false);
        return mProgressDialog;
    }

}
